import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class PunchTable {
    private final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    private Date late = null;       // 最迟打卡时间
    private ConcurrentHashMap<Long, PunchItem> punchList = null;   // 打卡表
    public PunchTable() {
        this.punchList = new ConcurrentHashMap<Long, PunchItem>();
        try {
            this.late = sdf.parse("09:01:01");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void addPunchByString(String s) {
        String[] res = s.split("\\|");
        // id
        Long id = Long.parseLong(res[0]);
        // date
        String timeString = res[2].trim();
        if (punchList.containsKey(id)) {
            // 人员不存在
            PunchItem p = punchList.get(id);
            p.punchByString(timeString, this.sdf);
        } else {
            // 人员已存在
            String name = res[1].trim();            // name
            PunchItem ci = new PunchItem(id, name); // 构造打卡
            ci.punchByString(timeString, this.sdf); // 添加打卡记录到单条记录
            this.punchList.put(id, ci);             // 添加单条记录到表记录中
        }
    }

    /*
     * @Description
     * @param pathname 文件路径
     * @param cs 输出文件格式
     * @return void
     * @author 阿鸣
     * @date 2022/3/18 19:07
     * @see
     */
    public void writeToFile(String pathname, String ePathname, String cs) {
        File file = new File(pathname);
        File eFile = new File(ePathname);
        // 排序
        Long[] keyArray = this.punchList.keySet().toArray(new Long[0]);
        Arrays.sort(keyArray);

        try {
            FileOutputStream out = new FileOutputStream(file);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, cs));

            FileOutputStream eOut = new FileOutputStream(eFile);
            BufferedWriter eBw = new BufferedWriter(new OutputStreamWriter(eOut, cs));
            for(Long key: keyArray){
                PunchItem item = this.punchList.get(key);
                if (item.getPunchOut() != null && item.getPunchIn() != null) {
                    bw.write(this.formatPunchString(item) + "\n");
                } else {
                    eBw.write(this.formatPunchString(item) + "\n");
                }
            }
            // TODO:AutoCloseable接口实现Buffer
            bw.close();
            eBw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void print() {
        for (Map.Entry<Long, PunchItem> entry : this.punchList.entrySet()) {
            System.out.println(entry.getKey().longValue());
            System.out.println(entry.getValue());
        }
    }

    /*
     * @Description 格式化单挑打卡信息并返回
     * @param item 打卡项
     * @return java.lang.String
     * @author 阿鸣
     * @date 2022/3/18 19:07
     * @see
     */
    private String formatPunchString(PunchItem item) {
        ArrayList<String> l = new ArrayList<String>();
        l.add(String.valueOf(item.getId()));
        l.add(item.getName());
        if (item.getPunchIn() != null) {
            l.add(sdf.format(item.getPunchIn()));
        } else {
            l.add("");
        }
        if (item.getPunchOut() != null) {
            l.add(sdf.format(item.getPunchOut()));
        } else {
            l.add("*");
        }
        l.add(this.getStatus(item));
        return String.join("|", l);
    }

    public String getStatus(PunchItem item) {
        // 按顺序检查打卡
        String s = "";
        Date punchIn = item.getPunchIn();
        Date punchOut = item.getPunchOut();
        if (punchIn == null || punchOut == null) {
            s = "缺卡";
            return s;
        }
        // 计算时间戳
        long diff = punchOut.getTime() - punchIn.getTime();
        long diffMinutes = diff / (1000 * 60);
        if (punchIn.after(this.late)) {
            s += "迟到";
        } else if (diffMinutes < 480) {
            s += "早退";
        } else {
            s += "正常";
        }
        return s;
    }

    public void toJsonFile(String filePath, String eFilePath, String cs) {

    }

}
